﻿using Sc.Domain;
using Sc.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Sc.Web.Controllers
{
    public class UseCarApplicationController : BaseController
    {
        /// <summary>
        /// 用车申请主页
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 查询我的申请
        /// </summary>
        /// <param name="pageSzie">显示条数</param>
        /// <param name="pageIndex">页码</param>
        /// <returns></returns>
        public JsonResult GetPageData(int pageSize, int pageIndex)
        {
            var data = new Car_UseCarApplicationService().GetList(UserLogin.UserId, pageSize, pageIndex);
            return Json(data, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 保存申诉
        /// </summary>
        /// <param name="id">业务ID</param>
        /// <param name="remark">申诉原因</param>
        /// <returns></returns>
        public int SaveAppeal(Car_UseCarApplication ent)
        {
            ent.UserId = UserLogin.UserId;
            ent.OrgzNo = UserLogin.OrgzNo;
            ent.IsComplaint = true;
            ent.InstanceState = 0;
            ent.Remark = Request.Params["o"];
            return new Car_UseCarApplicationService().SaveAppeal(ent);
        }

        /// <summary>
        /// 用车详情
        /// </summary>
        /// <returns></returns>
        public ActionResult UseCarAppeal(int id)
        {
            Domain.Car_UseCarApplication ent = new Car_UseCarApplicationService().GetDetail(id);
            ViewData["ent"] = ent;
            if (ent.OldId > 0)
            {
                ViewData["old"] = new Car_UseCarApplicationService().GetDetail(ent.OldId);
            }
            if (ent.InstanceState == 4 || ent.InstanceState == 5)
                ViewData["sendcarlist"] = new SendCarRecordService().GetSCRecord(ent.Id, 1);
            else
                ViewData["sendcarlist"] = null;
            return View();
        }
        /// <summary>
        /// 取消用车
        /// </summary>
        /// <returns></returns>
        public int CancelUseApplication(int id)
        {
            return new Car_UseCarApplicationService().CancelUseApplication(id);
        }
        /// <summary>
        /// 用车详情（审批）
        /// </summary>
        /// <returns></returns>
        public ActionResult UseCarInfo(string sid, int todo)
        {
            int id = 0;
            bool b = int.TryParse(sid.Substring(4), out id);
            Domain.Car_UseCarApplication ent = new Car_UseCarApplicationService().GetDetail(id);
            ViewData["ent"] = ent;
            if (ent.OldId > 0)
            {
                ViewData["old"] = new Car_UseCarApplicationService().GetDetail(ent.OldId);
            }
            if (ent.InstanceState == 4|| ent.InstanceState == 5)
                ViewData["sendcarlist"] = new SendCarRecordService().GetSCRecord(ent.Id, 1);
            else
                ViewData["sendcarlist"] = null;

            ViewData["isApproal"] = CurrUserIsApproal(id, this.UserLogin.UserId);
            ViewData["userid"] = this.UserLogin.UserId;
            ViewData["todo"] = todo;
            return View();
        }

        /// <summary>
        /// 用车详情（审批）
        /// </summary>
        /// <returns></returns>
        public ActionResult UseCarInfo2(string sid, int todo)
        {
            int id = 0;
            bool b = int.TryParse(sid.Substring(4), out id);
            Domain.Car_UseCarApplication ent = new Car_UseCarApplicationService().GetDetail(id);
            ViewData["ent"] = ent;
            if (ent.InstanceState == 4 || ent.InstanceState == 5)
                ViewData["sendcarlist"] = new SendCarRecordService().GetSCRecord(ent.Id, 1);
            else
                ViewData["sendcarlist"] = null;
            ViewData["isApproal"] = CurrUserIsApproal(id, this.UserLogin.UserId);
            ViewData["userid"] = this.UserLogin.UserId;
            ViewData["todo"] = todo;
            return View();
        }

        public object CurrUserIsApproal(int id, int userId)
        {
            var ent = new Car_UseCarApplicationService().GetDetail(id);
            if (ent.InstanceState == 2)
            {
                return 0;
            }
            return CommService.IsApproval(ent.ApproalUsers, userId, ent.NodeCount);
        }

        /// <summary>
        /// 我的代办
        /// </summary>
        /// <returns></returns>
        public ActionResult Todo()
        {
            return View();
        }

        public ActionResult Done()
        {
            return View();
        }


        /// <summary>
        /// 查询我的申请
        /// </summary>
        /// <param name="pageSzie">显示条数</param>
        /// <param name="pageIndex">页码</param>
        /// <returns></returns>
        public JsonResult GetTodoData(int pageSize, int pageIndex)
        {
            var data = new Sys_ToDoListService().GetList(this.UserLogin.UserId, pageSize, pageIndex, 0);
            return Json(data, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 查询我的已办
        /// </summary>
        /// <param name="pageSzie">显示条数</param>
        /// <param name="pageIndex">页码</param>
        /// <returns></returns>
        public JsonResult GetDoneData(int pageSize, int pageIndex)
        {
            var data = new Sys_ToDoListService().GetList(this.UserLogin.UserId, pageSize, pageIndex, 1);
            return Json(data, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 用车申诉
        /// </summary>
        /// <returns></returns>
        public ActionResult Comp()
        {
            var id = Convert.ToInt32(Request.Params["id"]);
            var ent = new Car_UseCarApplicationService().GetDetail(id);
            ViewData["ent"] = ent;
            return View();
        }
    }
}